package org.thirdHero.wx.config;

import cn.binarywang.wx.miniapp.api.WxMaService;
import com.github.binarywang.wxpay.service.WxPayService;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.thirdHero.core.token.RedisLockManager;

import javax.annotation.Resource;

/**
 * 买家端启动完成后执行该方法
 */
@Service
public class AppStartFinish implements ApplicationListener<ContextRefreshedEvent> {
    private final Logger logger = LoggerFactory.getLogger(AppStartFinish.class);
    @Resource
    private WxMaService wxMaService;
    @Resource
    private WxPayService wxPayService;
    @Resource
    private RedisTemplate redisTemplate;
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        if (contextRefreshedEvent.getApplicationContext().getParent() == null) {//保证只执行一次
            String appId = wxMaService.getWxMaConfig().getAppid();
            logger.info("加载微信小程序配置信息，appid为:{}", appId);
            String mchId = wxPayService.getConfig().getMchId();
            logger.info("加载微信小程序支付的muchId为:{}",mchId);
        }
        initGoodStock();
    }

    /**
     * 初始化产品数量
     */
    public void initGoodStock(){
        redisTemplate.opsForValue().set(RedisLockManager.REDIS_LOCK_TIME_OUT, "300000");
        logger.info("======================加载redis内容======================");
    }

}
